define(["app", 
        "jquery",
        "md5","ecb",
        "resources/UserResource", 
        "resources/company/CompanyResource", 
        "services/MenuService",
        "services/UserService"
], function(app) {
	
	var deps = ["$scope", "UserResource","$timeout","$location", "UserService", "MenuService","CompanyResource"];
	
	function controller($scope, UserResource, $timeout,$location, UserService,MenuService,CompanyResource) {
		/**
		 * 监听回车键，直接登陆
		 */
		$scope.enterSearch = function($event){
            if ($event.keyCode === 13) {
                $scope.login();
            }
        };
        
        function showError(message){
        	$scope.errorMessage=message;
        	var show = function(){
				$scope.errorMessage="";
			}
			$timeout(show,3000);
        }
        
        $scope.isPoneAvailable =function (){
	       	 var mobile =$scope.companyVo.mobile;
	       	 var myreg=/^[1][3,4,5,7,8,9][0-9]{9}$/;
	            if (!myreg.test(mobile)) {
	            	showError("手机号格式不 正确")
	            } else {
	                return true;
	            }
         }
        
        /**
         * 企业注册服务
         */
        $scope.register =function(){
        	
        	if($scope.companyVo==null){
        		showError("请填完善相应信息");
        		return;
        	}
        	if($scope.companyVo.mobile == null){
				showError("请输入手机号");
        		return;
        	}
        	if($scope.companyVo.password == null){
        		showError("请输入密码");
        		return;
        	}
        	
        	if($scope.companyVo.repassword == null){
        		showError("请再次输入密码");
        		return;
        	}
        	
        	if($scope.companyVo.repassword != $scope.companyVo.password){
        		showError("两次密码不一致");
        		return;
        	}
        	
        	if($scope.companyVo.checkCode == null){
        		showError("请输入验证码");
        		return;
        	}
        	$scope.companyVo.password=encrypt($scope.companyVo.password, $scope.companyVo.mobile)
        	CompanyResource.save($scope.companyVo).success(function(data){
				
        		if(data.resCode=='0000'){
        			$location.path("/companylogin");
        		}
        		else{
            		showError(data.message);
        			return;
        		}
            });
		};
		
		/**
		 * 找回密码
		 */
		$scope.dofindPassword =function(){
        	
        	if($scope.companyVo==null){
        		showError("请填完善相应信息");
        		return;
        	}
        	if($scope.companyVo.mobile == null){
        		showError("请输入手机号");
        		return;
        	}
        	if($scope.companyVo.password == null){
        		showError("请输入密码");
        		return;
        	}
        	
        	if($scope.companyVo.repassword == null){
        		showError("请再次输入密码");
        		return;
        	}
        	
        	if($scope.companyVo.repassword != $scope.companyVo.password){
        		showError("两次密码不一致");
        		return;
        	}
        	
        	if($scope.companyVo.checkCode == null){
        		showError("请输入验证码");
        		return;
        	}
        	$scope.companyVo.password=encrypt($scope.companyVo.password, $scope.companyVo.mobile)
        	CompanyResource.findPassword($scope.companyVo).success(function(data){
				
        		if(data.resCode=='0000'){
        			$location.path("/companylogin");
        		}
        		else{
            		showError(data.message);
        			return;
        		}
            });
		};
		
		
		
		/**
		 *  进入登录页面
		 */
		$scope.companylogin =function(){
			$location.path("/companylogin");
		};
		
		/**
		 * 进入注册页面
		 */
		$scope.initregister =function(){
			$location.path("/company");
		};
		/**
		 * 获取验证码
		 * 手机号
		 * 短信类型 1：小程序 2：企业端
		 */
		$scope.sendSms =function(){
			CompanyResource.sendSms($scope.companyVo.mobile,2).success(function(data){
        		// 返回成功
        		alert("发送成功！");
            });
		};
		
		/**
		 * 找回密码
		 */
		$scope.findPassword =function(){
			$location.path("/findPassword");
		};
		
        
		/**
		 *  企业登录
		 */
		$scope.login =function(){

			CompanyResource.login(
				$scope.companyVo.mobile,
				encrypt($scope.companyVo.password,$scope.companyVo.mobile)).success(
				function(data) {
					var user=data.obj;
					if(user ==null || user==undefined){
						if(data.resCode=='9999'){
							$scope.errorMessage = data.msg;
							var show = function(){
								$scope.errorMessage="";
							}
							$timeout(show,3000);
							return;
						}
					}
					// 返回成功
					if(user.authenticated) {
						
						UserService.setCurrentUser(user);
						
						$scope.menuConfig = MenuService.companyConfig;
						
						if(user.menus && user.menus.length > 0){
							var url = "";
							//判断跳转页面
							var companyAuthentication =user.compamnyAuthentication;
							if(companyAuthentication==0){
								$location.path("/companyAuthentication");
							}
							else{
								mymenu = user.menus[0];
								if(mymenu.subMenus && mymenu.subMenus.length > 0){
									url = $scope.menuConfig[mymenu.subMenus[0].markName].locationURL;
								} else {
									url = $scope.menuConfig[mymenu.markName].locationURL;
								}
								$location.path(url);
							}
						}
					} else {
						$scope.errorMessage = user.msg;
						UserService.removeCurrentUser();
					}
				});
		};
		
		/** 监听用户名是否为空 **/
		$scope.$watch('companyVo.userName', function(value) {
			if($scope.errorMessage){
				if(value==null){
	            	$scope.errorMessage=false;
	            }
			}
        });
		/** 监听密码是否为空 **/
		$scope.$watch('companyVo.password', function(value) {
			if($scope.errorMessage){
				if(value==null){
	            	$scope.errorMessage=false;
	            }
			}
        });
	}
	
	/**
	 * 密码 encrypt 加密
	 */
	function encrypt(content,srcKey){
	      var keyLen = 8;
		  var srcKeyLen = srcKey.length;
		
		 if(srcKeyLen > keyLen){
			 srcKey = srcKey.substr(0,keyLen);
			
		 }else{
			 var paddLen = keyLen -  srcKeyLen;
			 for(var i=0;i<paddLen;i++){
				 srcKey += "0";
			 }
		 }
		
		 var key = CryptoJS.enc.Utf8.parse(srcKey);		
		 var encryptStr =  CryptoJS.DES.encrypt(content, key, {mode: CryptoJS.mode.ECB,padding: CryptoJS.pad.Pkcs7});
		return encryptStr.toString();
	}
	controller.$inject = deps;
	app.lazy.controller("LoginController", controller);
});
